package com.yunwaikeji.gy_tool_library.utils.file.copy;

import android.util.Log;

import com.yunwaikeji.gy_tool_library.utils.file.GYFileLoadListener;
import com.yunwaikeji.gy_tool_library.utils.file.GYFileUtils;

import java.io.File;

public class GYFileCopyBuilder {
    private File oldFile;
    private File newFile;
    private GYFileCopyCallback fileCopyCallback;

    public GYFileCopyBuilder() {
    }

    public GYFileCopyBuilder(File oldFile, File newFile) {
        this.oldFile = oldFile;
        this.newFile = newFile;
    }

    public GYFileCopyBuilder(String oldFile, String newFile) {
        this(new File(oldFile), new File(newFile));
    }

    public GYFileCopyBuilder setOldFile(File oldFile) {
        this.oldFile = oldFile;
        return this;
    }

    public GYFileCopyBuilder setNewFile(File newFile) {
        this.newFile = newFile;
        return this;
    }

    public GYFileCopyBuilder setFileCopyCallback(GYFileCopyCallback fileCopyCallback) {
        this.fileCopyCallback = fileCopyCallback;
        return this;
    }

    public void build() {
        GYFileUtils.newFileWriteTask()
                .setReStart(true)
                .setFile(oldFile)
                .setWriteFile(newFile)
                .setFileLoadListener(new GYFileLoadListener() {
                    @Override
                    public void start() {

                    }

                    @Override
                    public void paused() {

                    }

                    @Override
                    public void stop() {

                    }

                    @Override
                    public void loading(long nowBytes, long totalBytes, long secondBytes) {

                    }

                    @Override
                    public void success(String file) {
                        if (fileCopyCallback != null) {
                            fileCopyCallback.success(oldFile, newFile);
                        }
                    }

                    @Override
                    public void error(String error) {
                        if (fileCopyCallback != null) {
                            fileCopyCallback.error(error);
                        }
                    }

                    @Override
                    public void finish() {

                    }
                })
                .start();
    }

    private void error(String error) {
        Log.e("GYFileCopyBuilder", error);
        if (fileCopyCallback != null) {
            fileCopyCallback.error(error);
        }
    }
}
